package utils;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ontology.concepts.general.ICase;
import ontology.concepts.vo.Situation;
import ontology.concepts.vo.VOCase;
import ontology.concepts.vo.VOLog;
import ontology.concepts.vo.VONorm;
import ontology.concepts.vo.VOOutcome;
import ontology.concepts.vo.VOResult;

public class DBUtils {
	
	public static ICase<Integer> getVOCase(String id){
		VOCase c = null;
		Session sess = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     c = (VOCase) sess.get(VOCase.class, id);
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return c;
	}
	
	public static void saveVOLog(VOLog log){
		Session s = HibernateUtil.getSessionFactory().openSession();
		Transaction t = s.beginTransaction();
			s.saveOrUpdate(log);
		t.commit();
		s.close();
	}

	public static List<VOCase> getVOCases(VONorm norm) {
		List<VOCase> cases = new ArrayList<VOCase>();
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     	Query query = sess.getNamedQuery("VOCase.findVOCaseByNorm");
				query = query.setString("normName", norm.getName());
				cases = query.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}

	public static List<VONorm> getNorms() {
		List<VONorm> norms = new ArrayList<VONorm>();
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     	Query query = sess.getNamedQuery("VONorm.findAll");
				norms = query.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return norms;
	}

	public static List<VOCase> getVOCasesByNormAction(String action) {
		Session sess = HibernateUtil.getSessionFactory().openSession();
		List<VOCase> cases = null;
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     Query query = sess.getNamedQuery("VOCase.findVOCaseByNormAction");
		     query = query.setString("actionName", action);
			 cases = query.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}

	public static List<VOCase> getVOCasesByNormArgs(VONorm norm) {
		Session sess = HibernateUtil.getSessionFactory().openSession();
		List<VOCase> cases = new ArrayList<VOCase>();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     for(Situation sit : norm.getAntecedent().getSituation()){
					Query query = sess.getNamedQuery("VOCase.findVOCaseByNormActionArgs");
					query = query.setString("actionArgs", sit.getVoaction().getName());
					cases.addAll(query.list());
				}
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}

	public static void saveOrUpdate(Object result) {
		 Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     sess.saveOrUpdate(result);
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
	}

	public static VOOutcome getVOOutcomeByVOCase(Long id) {
		VOOutcome outcome = null;
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     Query q = sess.getNamedQuery("VOCase.findVOOutcomeByVOCase");
		     q = q.setLong("caseId", id);
		     outcome = (VOOutcome) q.list().get(0);
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return outcome;
	}

	public static List<VOCase> getVOCasesByContractType(VONorm norm) {
		List<VOCase> cases = null;
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     Query q = sess.getNamedQuery("VOCase.findVOCaseByContractType");
		     q = q.setString("contractType", norm.getContract_type());
		     cases = q.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}

	public static List<VOCase> getVOCasesByNorm(VONorm norm) {
		List<VOCase> cases = null;
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     Query q = sess.getNamedQuery("VOCase.findVOCaseByNorm");
		     q = q.setString("normName", norm.getName());
		     cases = q.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}
	
	public static List<VOCase> getVOCases() {
		List<VOCase> cases = null;
		Session sess = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = sess.beginTransaction();
		     Query q = sess.getNamedQuery("VOCase.findAll");
		     cases = q.list();
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		 }
		 finally {
		     sess.close();
		 }
		 return cases;
	}
	
	public static void main(String []args){
		VONorm norm = new VONorm();
		norm.setContract_type("supply-agreement");
		//List<VOCase> cases = DBUtils.getVOCasesByContractType(norm);
		VOOutcome cases = DBUtils.getVOOutcomeByVOCase(1L);
		System.out.println(cases.toString());
		//		for(VOCase voc : cases){
//			System.out.println(voc.toString());
//		}
	}
}
